package com.gisuni.sddc.controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.service.UserOrganizationService;
import com.gisuni.sddc.model.Organization;
import com.gisuni.sddc.model.Response;
import com.gisuni.sddc.model.UserOrganization;

@IocBean(singleton = false)
@At("/client/service/userOrganizationController")
public class UserOrganizationController {

	private Log log = LogFactory.getLog(UserOrganizationController.class);

	@Inject
	private UserOrganizationService  userOrganizationService;

	
	/**
	 * 查询所有人员-组织数据
	 * 
	 * @return
	 */
	@At("/queryUserAll")
	@GET
	@POST
	@Ok("json")
	public Object queryBaseUserAll(@Param("userName") String userName,@Param("page") int page, @Param("rows") int rows,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			int from=(page-1)*rows;
			res = userOrganizationService.queryBaseUserAll(from,from+rows,userName);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	
	/**
	 * 修改数据
	 * @param UserId
	 * @param UserName
	 * @param TELEPHONE
	 * @return
	 */
	@At("/updateUserOrganization")
	@GET
	@POST
	@Ok("json")
	public Object updataBaseUser(@Param("id") Long id,@Param("companyId") String companyId,@Param("businessId") String businessId,@Param("developId") String developId, @Param("existFlag")String existFlag ,HttpServletRequest request) {
		
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			UserOrganization userOrganization=new UserOrganization();
			userOrganization.setUserId(id);
			userOrganization.setOrganizationCompanyId(companyId); 
			userOrganization.setOrganizationDevelopId(developId);
			userOrganization.setOrganizationBusinessId(businessId);
			res = userOrganizationService.updateUserOrganization(userOrganization,existFlag);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	/**
	 * 查询组织数据
	 * @param parentId
	 * @return
	 */
	
	@At("/getOrganization")
	@GET
	@POST
	@Ok("json")
	public Object getOrganization(@Param("parentId") String parentId){
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			Organization organization = new Organization();
			organization.setParentId(parentId);
			res = userOrganizationService.getOrganization(organization);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}

	/**
	 * 删除数据
	 * 
	 * @param UserId
	 * @return
	 
	@At("/deleteBaseUser")
	@GET
	@POST
	@Ok("json")
	public Object deleteBaseUser(@Param("UserId") int Id, HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			ClientUser baseUser=new ClientUser();
			baseUser.setId(Id);
			res = clientUserService.deleteBaseUser(baseUser);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}*/

	/**
	 * 添加数据
	 * 
	 * @param UserName
	 * @param UserNote
	 * @return
	 
	@At("/addBaseUser")
	@GET
	@POST
	@Ok("json")
	public Object addBaseUser(@Param("regionid") int Regionid,@Param("depotid") int Depotid,@Param("type") int Type,@Param("loginname") String Loginname, @Param("password") String Password,@Param("name") String Name,@Param("telephone") String Telephone,@Param("sex") int Sex,@Param("depart") String Depart,@Param("areaid") int Areaid,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			ClientUser baseUser=new ClientUser();
			baseUser.setdepart(Depart);
			baseUser.setloginname(Loginname);
			baseUser.setName(Name);
			baseUser.setareaid(Areaid);
			baseUser.setsex(Sex);
			baseUser.settelephone(Telephone);
			baseUser.setpassword(Password);
			baseUser.settype(Type);
			baseUser.setdepotid(Depotid);
			baseUser.setregionid(Regionid);
			res = clientUserService.addBaseUser(baseUser);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
*/


	 
 
	 
//	
//	@At("/queryBaseUser")
//	@GET
//	@POST
//	@Ok("json")
//	public Object queryBaseUser(@Param("username") String username, @Param("password") String password,HttpServletRequest request) {
//		Response res = new Response();
//		res.setSuccess(false);
//		Object r = res;
//		try {
//			ClientUser baseUser=new ClientUser();
//			baseUser.setpassword(password);
//			baseUser.setloginname(username);
//			res = clientUserService.queryBaseUser(baseUser,request);
//			JSONObject o = Response.toJSONObject(res);
//			ObjectMapper mapper = new ObjectMapper();
//			r = mapper.readValue(o.toString(), Map.class);
//		} catch (Exception e) {
//			log.error(e.getMessage());
//		}
//		return r;
//	}
}
